#!/usr/bin/env node

const http=require('https'),
      log=console.log,
      user=process.argv[2]||'tung-2019',
      url=require('url');

const addr=`https:\/\/gitee.com/api/v5/users/${user}/repos?access_token=0a22e3ef823b41712e42fc9adfa108c5&type=all&sort=full_name&page=1&per_page=20`,
    options = url.parse(addr);

    options.headers = {
      'User-Agent': '03-get-repos.js'
    };

http.get(options,(res)=>{
 /* log(`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`);

  log(res.headers);
  log();
*/
  let result='';//最终数据
  res.on('data',data=>{
    result+=data.toString('utf8');
    
  });
  
  res.on('end',()=>{
    let reps=JSON.parse(result);
    log('Total:',reps.length);
    log('=======================');

    //for(const repos of reps){
    for(let i=0;i<reps.length;i++){
      log('%d\t%s',i,reps[i].name);
    }
    /*
     * github仓库适用
    log('Total:',reps.items.length);
    log('========================');
    for(let i=0;i<reps.items.length;i++){
      log('%d\t%s',(i+1),reps.items[i].name);
    }
    */
  });
});


